home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / DirectMusic / Tutorials / Tut1 / tutorial1.cpp next >
C/C++ Source or Header  |  2001-10-31  |  4KB  |  102 lines

  1. //-----------------------------------------------------------------------------
  2. // File: play.cpp
  3. //
  4. // Desc: DirectMusic tutorial to show how to play a segment 
  5. //       on the default audio path
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define INITGUID
  10. #include <windows.h>
  11. #include <dmusicc.h>
  12. #include <dmusici.h>
  13.  
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Defines, constants, and global variables
  19. //-----------------------------------------------------------------------------
  20. IDirectMusicLoader8*      g_pLoader         = NULL;
  21. IDirectMusicPerformance8* g_pPerformance    = NULL;
  22. IDirectMusicSegment8*     g_pSegment        = NULL;
  23.  
  24.  
  25.  
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Name: WinMain()
  29. // Desc: Plays a single wave file using DirectMusic on the default audio path.
  30. //-----------------------------------------------------------------------------
  31. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  32.                       INT nCmdShow )
  33. {
  34.     // Initialize COM
  35.     CoInitialize(NULL);
  36.     
  37.     // Create loader object
  38.     CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
  39.                       IID_IDirectMusicLoader8, (void**)&g_pLoader );
  40.  
  41.     // Create performance object
  42.     CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
  43.                       IID_IDirectMusicPerformance8, (void**)&g_pPerformance );
  44.  
  45.     // Initialize the performance with the standard audio path.
  46.     // This initializes both DirectMusic and DirectSound and 
  47.     // sets up the synthesizer. 
  48.     g_pPerformance->InitAudio( NULL, NULL, NULL, 
  49.                                DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
  50.                                DMUS_AUDIOF_ALL, NULL );
  51.  
  52.     CHAR strPath[MAX_PATH];
  53.     GetWindowsDirectory( strPath, MAX_PATH );
  54.     strcat( strPath, "\\media" );
  55.  
  56.     // Tell DirectMusic where the default search path is
  57.     WCHAR wstrSearchPath[MAX_PATH];
  58.     MultiByteToWideChar( CP_ACP, 0, strPath, -1, 
  59.                          wstrSearchPath, MAX_PATH );
  60.  
  61.     g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes, 
  62.                                    wstrSearchPath, FALSE );
  63.     
  64.     // Load the segment from the file
  65.     WCHAR wstrFileName[MAX_PATH] = L"The Microsoft Sound.wav";   
  66.     if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
  67.                                                IID_IDirectMusicSegment8,
  68.                                                wstrFileName,
  69.                                                (LPVOID*) &g_pSegment ) ) )
  70.     {
  71.         MessageBox( NULL, "Media not found, sample will now quit", 
  72.                           "DirectMusic Tutorial", MB_OK );
  73.         return 0;
  74.     }
  75.  
  76.     // Download the segment's instruments to the synthesizer
  77.     g_pSegment->Download( g_pPerformance );
  78.  
  79.     // Play segment on the default audio path
  80.     g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, 0, 
  81.                                    0, NULL, NULL, NULL );
  82.  
  83.     // Now DirectMusic will play in the backgroud, 
  84.     // so continue on with our task
  85.     MessageBox( NULL, "Click OK to Exit.", "DirectMusic Tutorial", MB_OK );
  86.  
  87.     // Stop the music, and close down 
  88.     g_pPerformance->Stop( NULL, NULL, 0, 0 );
  89.     g_pPerformance->CloseDown();
  90.  
  91.     // Cleanup all interfaces
  92.     g_pLoader->Release(); 
  93.     g_pPerformance->Release();
  94.     g_pSegment->Release();
  95.  
  96.     // Close down COM
  97.     CoUninitialize();
  98.         
  99.     return 0;
  100. }
  101.  
  102.